package com.sky.service.impl;

import com.sky.dto.SeriesDTO;
import com.sky.entity.Series;
import com.sky.entity.SeriesBook;
import com.sky.mapper.SeriesMapper;
import com.sky.mapper.SeriesBookMapper;
import com.sky.service.SeriesService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;

/**
 * @Description: 书籍系列实现类
 * @Author: Qiuyan
 * @Date: 2025-04-13 10:59
 * @Version： 1.0
 **/

/**
 * 书籍系列管理服务
 */
@Service
@Slf4j
public class SeriesServiceImpl implements SeriesService {

    @Autowired
    private SeriesMapper seriesMapper;
    
    @Autowired
    private SeriesBookMapper seriesBookMapper;

    /**
     * 新增系列
     * @param seriesDTO
     */
    @Override
    @Transactional
    public void saveWithBooks(SeriesDTO seriesDTO) {
        Series series = new Series();
        BeanUtils.copyProperties(seriesDTO, series);

        //插入系列数据
        seriesMapper.insert(series);

        //获取生成的系列id
        Integer seriesId = series.getId();

        List<SeriesBook> seriesBooks = seriesDTO.getSeriesBooks();
        if (seriesBooks != null && seriesBooks.size() > 0) {
            seriesBooks.forEach(seriesBook -> {
                seriesBook.setSeriesId(seriesId);
            });
            //保存系列和书籍的关联关系
            seriesBookMapper.insertBatch(seriesBooks);
        }
    }
}
